import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/config/address.dart';
import 'package:xvideos/hj_utils/api_service/mine_service.dart';
import 'package:xvideos/hj_utils/http_manager.dart';
import 'package:xvideos/tools_base/debug_log.dart';

import '../../../../hj_utils/global.dart';
import '../../make_money/alipay_ccdcapi_model.dart';

//添加银行卡
class AdddBankPage extends StatefulWidget {
  const AdddBankPage({super.key});

  @override
  State<AdddBankPage> createState() => _AdddBankPageState();
}

class _AdddBankPageState extends State<AdddBankPage> {
  TextEditingController bankNameController = TextEditingController();
  TextEditingController cardUserNameController = TextEditingController();
  TextEditingController bankNumController = TextEditingController();

  ///校验银行卡信息
  void _bankNumberVerifyReq() async {
    String bankNum = bankNumController.text.trim();
    if (bankNum.isEmpty || bankNum.length < 13) {
      showToast("银行卡号错误");
      return;
    }
    httpManager.fetchResponseByGET(Address.ALI_CCD_API + bankNum).then((ret) {
      if (!ret.isSuccess) {
        showToast("银行卡验证失败.");
        return;
      }
      if (ret.isSuccess) {
        final model = ApcApiModel.fromMap(Map<String, dynamic>.from(ret.data));
        if (model?.validated ?? false) {
          // model.bank, model.cardType
          debugLog("model.bank-->", "${model?.bank}");
          debugLog("model.cardType-->", "${model?.cardType}");
          // _commonWithdrawReq(true, bank: model?.bank);
          _addBankCard(model!);
        } else {
          showToast("银行卡验证失败");
        }
      } else {
        showToast("银行卡验证失败");
      }
    });
  }

  _addBankCard(ApcApiModel model) async {
    final res = await MineService.getAddBankCard(model.key, bankNameController.text, model.bank, model.cardType);
    if (res) {
      Get.back(result: true);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('添加银行卡'),
      ),
      body: Column(
        children: [
          _buildInputWidget("银行名称", bankNameController),
          _buildInputWidget("银行卡号", bankNumController),
          _buildInputWidget("持卡人名称", cardUserNameController),
          Spacer(),
          // Rectangle 2784
          InkWell(
            onTap: () => _bankNumberVerifyReq(),
            child: Container(
              margin: EdgeInsets.symmetric(vertical: 20, horizontal: 16),
              height: 47,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(4)),
                  gradient: LinearGradient(colors: [const Color(0xff84a4f9), const Color(0xff2b5dde)])),
              child: Center(
                child: // 确定
                    Text(
                  "确定",
                  style: const TextStyle(color: const Color(0xffffffff), fontWeight: FontWeight.w600, fontSize: 14.0),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }

  _buildInputWidget(String title, TextEditingController controller) {
    return Container(
      height: 70,
      margin: EdgeInsets.symmetric(horizontal: 16, vertical: 10),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            title,
            style: const TextStyle(color: const Color(0xffffffff), fontWeight: FontWeight.w900, fontSize: 12.0),
            textAlign: TextAlign.center,
          ),
          SizedBox(height: 10),
          Container(
            height: 42,
            padding: EdgeInsets.only(left: 10),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(2)),
              color: const Color(0xff202733),
            ),
            child: TextField(
              controller: controller,
              maxLines: 1,
              autocorrect: true,
              autofocus: false,
              style: TextStyle(fontSize: 14, color: Colors.white, textBaseline: TextBaseline.alphabetic),
              decoration: InputDecoration(
                hintStyle: TextStyle(color: Color(0xff434c55), fontSize: 14),
                hintText: "请输入",
                border: InputBorder.none,
              ),
            ),
          )
        ],
      ),
    );
  }
}
